<?php
class Server {
  public static function login($orm, $sys, $inst, $is_iframe) {
    $url = Utils::get_param('url', '');
    $rt_url = Utils::get_param('rt_url', '');
    $user = Utils::get_param('user', '');
    $pass = Utils::get_param('pass', '');
    $url = $url . (($rt_url != '') ? '&u=' . urlencode($rt_url) : '');
    if ($user == '') {
      $_SESSION['lgnerr'] = 'Username is empty';
      Utils::redirect($url, $is_iframe);
      return;
    }
    
    unset($_SESSION[SYS . $sys]->is_authorised);
    
    $opts = new stdClass();
    $opts->cd = 't.account = :v1';
    $opts->cdv = array(':v1' => $user);
    $rslt = $orm->get('pb_ecom_admin', NULL, NULL, NULL, NULL, $opts)->d;
    if (count($rslt) == 0) {
      $_SESSION['lgnerr'] = 'Username not found';
      Utils::redirect($url, $is_iframe);
      return;
    }
    
    if ($rslt[0]->password != md5($pass)) {
      $_SESSION['lgnerr'] = 'Password incorrect';
      Utils::redirect($url, $is_iframe);
      return;
    }
    
    unset($_SESSION['lgnerr']);
    $_SESSION[SYS . $sys]->is_authorised = 1;
    Utils::redirect($url, $is_iframe);
  }
}
?>